package indexer;

import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import databaseconnector.DBProcessor;


/*
 * 中文租房索引建立器
 */
public class Rent_index {

	//MySQL数据库连接器
	private static DBProcessor processor;
	
	//构造函数
	public Rent_index()
	{
		processor = new DBProcessor();
	}

	//从Rent数据库抽取数据建立索引
	public static void buildRentIndex(boolean create) throws IOException {
		
		//索引输出目录
		Directory dir = FSDirectory.open(new File("E:/Index/RentIndex"));
		
		IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_40,
				new SmartChineseAnalyzer(Version.LUCENE_40));
		
		if (create) {
			cfg.setOpenMode(OpenMode.CREATE);
		} else {
			cfg.setOpenMode(OpenMode.CREATE_OR_APPEND);
		}
		
		IndexWriter writer = new IndexWriter(dir, cfg);
		ResultSet rlt = processor.getRent();
		Document doc = new Document();
		
		//Field域构建
		Field id = new StoredField("id", Integer.MIN_VALUE);
		Field price = new IntField("price", Integer.MIN_VALUE, Field.Store.YES);
		
		//Field house = new StoredField("house", "");
		//将house信息分解成room个数和hall个数
		Field room = new IntField("room", 0, Field.Store.YES);
		Field hall = new IntField("hall", 0, Field.Store.YES);
		
		Field decoration = new TextField("decoration", "",Field.Store.YES);
		Field area = new IntField("area", Integer.MIN_VALUE, Field.Store.YES);
		Field year = new IntField("year", Integer.MIN_VALUE, Field.Store.YES);
		
		//将stair信息分成“高、中、低”楼层信息和具体楼层数信息
		Field stair = new TextField("stair", "",Field.Store.YES);
		Field stair_num = new IntField("stair_num", 0, Field.Store.YES);
		
		Field build = new TextField("build", "", Field.Store.YES);
		Field direction = new TextField("direction", "", Field.Store.YES);
		Field region = new TextField("region", "", Field.Store.YES);
		Field imglink = new StoredField("imglink", "");
		
		
		
		//将Field域加入到doc中
		doc.add(id);
		doc.add(price);
		doc.add(room);
		doc.add(hall);
		doc.add(decoration);
		doc.add(area);
		doc.add(year);
		doc.add(stair);
		doc.add(stair_num);
		doc.add(build);
		doc.add(direction);
		doc.add(region);
		doc.add(imglink);
		
		
		//为doc的各Field赋值
		String strPrice,strArea,strYear;
		String tmpStr;

		try {
			while (rlt.next()) {
				
				//设置id值
				id.setIntValue(rlt.getInt(1));
				System.out.print(rlt.getInt(1) +  " ");
				
				//Id超过41595后为英文租房信息，需要进行其他处理
				if(rlt.getInt(1) > 41595){
					break;
				}
				
				//设置price值
				try
				{
					strPrice = rlt.getString(2);
					price.setIntValue(Integer.parseInt(strPrice.substring(0,
							strPrice.length() - 3)));
				}
				catch(Exception e){}
				
				//设置house值
				String tempHouse = rlt.getString(3);
				int tempRoom = 0;
				int tempHall = 0;
				if(tempHouse.contains("室"))
				{
					tempRoom = Integer.parseInt(tempHouse.substring(0, tempHouse.indexOf("室")));
					room.setIntValue(tempRoom);
					if(tempHouse.contains("厅"))
					{
						tempHall = Integer.parseInt(tempHouse.substring(tempHouse.indexOf("室")+1,
								tempHouse.indexOf("厅")));
						hall.setIntValue(tempHall);
					}
				}
				//System.out.print(tempRoom + " " + tempHall + " ");
				
				//设置decoration值
				tmpStr = rlt.getString(4);
				if(tmpStr != null){
					decoration.setStringValue(tmpStr);
				}
				
				//设置area值
				strArea = rlt.getString(5);
				area.setIntValue(Integer.parseInt(strArea.substring(0,
						strArea.length() - 2)));
				
				//设置year值
				strYear = rlt.getString(6);
				year.setIntValue(Integer.parseInt(strYear.substring(0,
						strYear.length() - 1)));
				
				//设置stair值
				tmpStr = rlt.getString(7);
				if(tmpStr != null)
				{
					String tempStair = tmpStr.substring(0, tmpStr.indexOf("楼层"));
					stair.setStringValue(tempStair);
					if(tmpStr.contains("楼层/"))
					{
						int front = tmpStr.indexOf("楼层/") + 3;
						int rear = tmpStr.indexOf("层", front);
						int tempStairNum = Integer.parseInt(tmpStr.substring(front, rear));
						stair_num.setIntValue(tempStairNum);
						//System.out.print(tempStair + " " + tempStairNum + " ");
					}
				}
				
				
				//设置build值
				tmpStr = rlt.getString(8);
				if(tmpStr != null){
					build.setStringValue(tmpStr);
				}
				
				//设置direction值
				tmpStr = rlt.getString(9);
				if(tmpStr != null){
					direction.setStringValue(tmpStr);
				}
				
				//设置region值
				tmpStr =rlt.getString(10);
				if(tmpStr != null){
					region.setStringValue(tmpStr);
				}
				
				//设置imglink值
				tmpStr = rlt.getString(12);
				if(tmpStr != null){
					imglink.setStringValue(tmpStr);
				}
				
				System.out.println();
				writer.addDocument(doc);

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (create) {
			writer.forceMerge(1);
		}
		writer.close();
	}
}
